import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:进⾏普通⽂件的复制
 * User: wangxin
 * Date: 2024-08-15
 * Time: 21:27
 */
public class test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入要复制的文件路径：");
        String sourcePath = scanner.next();
        File sourceFile = new File(sourcePath);
        if(!sourceFile.exists()){
            System.out.println("文件不存在或输入有误！");
            return;
        }
        if(!sourceFile.isFile()){
            System.out.println("文件不是普通文件！");
            return;
        }
        System.out.print("输入要复制的目标路径：");
        String destPath = scanner.next();
        File destFile = new File(destPath);
        if(destFile.exists()){
            if(destFile.isDirectory()){
                System.out.println("目标路径已存在，是一个目录，请确定输入路径是否正确！");
                return;
            }
            if(destFile.isFile()){
                System.out.println("目标路径文件已存在，是否进行覆盖：y/n");
                String ans = scanner.next();
                if(!ans.toLowerCase().contains("y")){
                    System.out.println("停止覆盖！");
                    return;
                }
            }
        }
        try(InputStream is = new FileInputStream(sourceFile)){
            try(OutputStream os = new FileOutputStream(destFile)){
                byte[] buf = new byte[1024];
                int len;
                while(true){
                    len = is.read(buf);
                    if(len == -1 ){
                        break;
                    }
                    os.write(buf);
                }
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("复制完成！");
    }
}
